import collections
class Solution(object):
    def customSortString(self, order, s):
        """
        :type order: str
        :type s: str
        :rtype: str
        """
        c = collections.Counter(s)
        t = ''
        for ch in order:
            if ch in c:
                t += ch * c[ch]
        for ch in c:
            if ch not in order:
                t += ch * c[ch]
        return t


data = Solution()
order = "cba"
s = "abcd"
print(data.customSortString(order, s))
